const {
    resolve
} = require('path')

const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    mode: 'development',
    entry: {
        base: './src/pages/base.js',
        login: './src/pages/login/index.js',
        userAdd: './src/pages/userAdd/index.js',
        userUpdate: './src/pages/userUpdate/index.js',
        users: './src/pages/users/index.js'
    },
    output: {
        path: resolve(__dirname, 'dist'),
        filename: 'js/[name]_[contenthash:8].js',
        clean: true
    },
    module: {
        rules: [
            // 处理js  ES6 => ES5
            {
                test: /\.js$/,
                exclude: /node_modules/, // 不进行处理的文件夹
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: [
                            [
                                '@babel/preset-env',
                                {
                                    useBuiltIns: 'usage', // 只打包使用的ES6新API的实现代码
                                    corejs: {
                                        version: 2
                                    } // 指定core-js的版本号为2
                                }
                            ]
                        ],
                        plugins: [], // 指定babel插件包
                    }
                }
            },
            // 处理CSS
            {
                test: /\.css$/, // 配置css文件的正则
                // 1. 每个loader就是一个处理函数
                // 2. 多个loader, 从下向上依次执行
                use: [
                    'style-loader', // js文件 => 页面的style标签
                    'css-loader' // css文件 => js文件
                ]
            },
            // 处理art
            {
                test: /\.art$/,
                loader: "art-template-loader",
            },
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/pages/index/index.html',
            filename: 'index.html',
            chunks: [],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/login/index.html',
            filename: 'login.html',
            chunks: ["base", "login"],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/userAdd/index.html',
            filename: 'userAdd.html',
            chunks: ["base", "userAdd"],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/userUpdate/index.html',
            filename: 'userUpdate.html',
            chunks: ["base", "userUpdate"],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/users/index.html',
            filename: 'users.html',
            chunks: ["base", "users"],
            inject: 'body'
        })
    ],
    devServer: {
        port: 5000,
        open: true
    }
}